package com.hluck.goodweatherapp.data.repository;

import android.util.Log;

import androidx.lifecycle.MutableLiveData;

import com.hluck.goodweatherapp.data.Constant;
import com.hluck.goodweatherapp.data.bean.AirResponse;
import com.hluck.goodweatherapp.data.bean.HourlyResponse;
import com.hluck.goodweatherapp.data.bean.WeatherData;
import com.hluck.goodweatherapp.data.bean.WeatherIndexResponse;
import com.hluck.goodweatherapp.data.bean.WeekWeather;
import com.hluck.goodweatherapp.data.network.api.ApiService;
import com.hluck.mvvmlibrary.network.ApiType;
import com.hluck.mvvmlibrary.network.NetworkApi;
import com.hluck.mvvmlibrary.network.observer.BaseObserver;

/**
 * @Author： LJH
 * @Time： 2024/1/2
 * @description：
 */
public class WeatherRepository {

    private static final String TAG = "WeatherRepository";

    private WeatherRepository() {
    }

    private static class SingleInstance{
        private static final WeatherRepository INSTANCE = new WeatherRepository();
    }

    public static WeatherRepository getInstance(){
        return WeatherRepository.SingleInstance.INSTANCE;
    }

    /**
     * 实况天气
     * @param responseLiveData 成功数据
     * @param failed        错误信息
     * @param cityId        城市ID
     */
    public void nowWeather(MutableLiveData<WeatherData> responseLiveData,MutableLiveData<String> failed,String cityId){

        String type = "实时天气-->";

        NetworkApi.createService(ApiService.class, ApiType.WEATHER).nowWeather(cityId)
                .compose(NetworkApi.applySchedulers(new BaseObserver<WeatherData>() {
            @Override
            public void onSuccess(WeatherData weatherData) {
                if (weatherData == null){
                    failed.postValue("实况天气数据为null，请检查城市ID是否正确。");
                    return;
                }

                //请求接口成功返回数据，失败返回状态码
                if (Constant.SUCCESS.equals(weatherData.getCode())){
                    responseLiveData.postValue(weatherData);
                } else {
                    failed.postValue(type+weatherData.getCode());
                }
            }

            @Override
            public void onFailure(Throwable e) {
                Log.e("TAG","onFailure:"+e.getMessage());
                failed.postValue(type+e.getMessage());
            }
        }));
    }


    /**
     * 天气预报
     * @param responseLiveData
     * @param failed
     * @param cityId
     */
    public void dailyWeather(MutableLiveData<WeekWeather> responseLiveData, MutableLiveData<String> failed, String cityId){

        String type = "天气预报-->";

        NetworkApi.createService(ApiService.class, ApiType.WEATHER).dailyWeather(cityId)
                .compose(NetworkApi.applySchedulers(new BaseObserver<WeekWeather>() {
                    @Override
                    public void onSuccess(WeekWeather weekWeather) {
                        if (weekWeather == null){
                            failed.postValue("天气数据为null，请检查城市ID是否正确。");
                            return;
                        }

                        //请求接口成功返回数据，失败返回状态码
                        if (Constant.SUCCESS.equals(weekWeather.getCode())){
                            responseLiveData.postValue(weekWeather);
                        } else {
                            failed.postValue(type+weekWeather.getCode());
                        }
                    }

                    @Override
                    public void onFailure(Throwable e) {
                        Log.e("TAG","onFailure:"+e.getMessage());
                        failed.postValue(type+e.getMessage());
                    }
                }));
    }



    public void lifestyle(MutableLiveData<WeatherIndexResponse> responseLiveData, MutableLiveData<String> failed, String cityId){

        String type = "生活指数-->";

        NetworkApi.createService(ApiService.class, ApiType.WEATHER).lifeStyle("1,2,3,4,5,6,7,8,9",cityId)
                .compose(NetworkApi.applySchedulers(new BaseObserver<WeatherIndexResponse>() {
                    @Override
                    public void onSuccess(WeatherIndexResponse weekWeather) {
                        if (weekWeather == null){
                            failed.postValue("生活指数数据为null，请检查城市ID是否正确。");
                            return;
                        }

                        //请求接口成功返回数据，失败返回状态码
                        if (Constant.SUCCESS.equals(weekWeather.getCode())){
                            responseLiveData.postValue(weekWeather);
                        } else {
                            failed.postValue(type+weekWeather.getCode());
                        }
                    }

                    @Override
                    public void onFailure(Throwable e) {
                        Log.e("TAG","onFailure:"+e.getMessage());
                        failed.postValue(type+e.getMessage());
                    }
                }));
    }


    /**
     * 逐小时天气
     * @param responseLiveData
     * @param failed
     * @param cityId
     */
    public void hourlyWeather(MutableLiveData<HourlyResponse> responseLiveData, MutableLiveData<String> failed, String cityId){

        String type = "逐小时天气预报-->";

        NetworkApi.createService(ApiService.class, ApiType.WEATHER).hourWeather(cityId)
                .compose(NetworkApi.applySchedulers(new BaseObserver<HourlyResponse>() {
                    @Override
                    public void onSuccess(HourlyResponse weekWeather) {
                        if (weekWeather == null){
                            failed.postValue("生活指数数据为null，请检查城市ID是否正确。");
                            return;
                        }

                        //请求接口成功返回数据，失败返回状态码
                        if (Constant.SUCCESS.equals(weekWeather.getCode())){
                            responseLiveData.postValue(weekWeather);
                        } else {
                            failed.postValue(type+weekWeather.getCode());
                        }
                    }

                    @Override
                    public void onFailure(Throwable e) {
                        Log.e("TAG","onFailure:"+e.getMessage());
                        failed.postValue(type+e.getMessage());
                    }
                }));
    }


    /**
     * 空气质量天气预报
     * @param responseLiveData
     * @param failed
     * @param cityId
     */
    public void airWeather(MutableLiveData<AirResponse> responseLiveData, MutableLiveData<String> failed, String cityId){

        String type = "空气质量天气预报-->";

        NetworkApi.createService(ApiService.class, ApiType.WEATHER).airWeather(cityId)
                .compose(NetworkApi.applySchedulers(new BaseObserver<AirResponse>() {
                    @Override
                    public void onSuccess(AirResponse weekWeather) {
                        if (weekWeather == null){
                            failed.postValue("空气质量预报数据为null，请检查城市ID是否正确。");
                            return;
                        }

                        //请求接口成功返回数据，失败返回状态码
                        if (Constant.SUCCESS.equals(weekWeather.getCode())){
                            responseLiveData.postValue(weekWeather);
                        } else {
                            failed.postValue(type+weekWeather.getCode());
                        }
                    }

                    @Override
                    public void onFailure(Throwable e) {
                        Log.e("TAG","onFailure:"+e.getMessage());
                        failed.postValue(type+e.getMessage());
                    }
                }));
    }

}
